package com.mrt.chatgpt.data.trigger.job;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.mrt.chatgpt.data.domain.order.service.IOrderService;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.CloseOrderRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 86135
 * @Date 2024/3/26 14:46
 * @Description
 * @Version 1.0
 * @快捷键 getterANDsetter ALT+Insert
 * @快捷键 Ctrl+/或者Ctrl+Shift+/都可以快速注释
 * @快捷键 Ctrl+D 快速复制代码
 * @“ctrl+r”快捷键,用于当前文件内容替换,指的是在当前打开的文件中替换匹配的字符,只操作一个文件;
 * @ALT+鼠标左键 整列编辑
 * @快捷键提示 ALT+Enter
 * @快捷键 快速写注释：在方法函数上方输入/** 然后回车
 * @快捷键 Ctrl+O，选择重写的方法
 * @快捷键 ALT+INSERT
 */
@Slf4j
@Component
public class TimeoutCloseOrderJob {

    @Resource
    private AlipayClient alipayClient;
    @Resource
    private IOrderService orderService;
    //TODO 待修改
    @Autowired(required = false)
    private NativePayService payService;
    @Value("${wxpay.config.mchid}")
    private String mchid;

    @Scheduled(cron = "0 0/10 * * * ?")
    public void exec(){
        try{
            List<String> orderIds = orderService.queryTimeoutCloseOrderList();
            if(orderIds.isEmpty()){
                log.info("定时任务，超时30分钟订单关闭，暂无超时未支付订单 orderIds is null");
                return;
            }
            for(String orderId : orderIds){
                boolean status = orderService.changeOrderClose(orderId);
                AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
                JSONObject bizContent = new JSONObject();
                bizContent.put("out_trade_no", orderId);
                request.setBizContent(bizContent.toString());
                AlipayTradeCloseResponse response = alipayClient.execute(request);
                if(response.isSuccess()){
                    log.info("调用成功");
                } else {
                    log.info("调用失败");
                }
                log.info("定时任务，超时30分钟订单关闭 orderId: {} status：{}", orderId, status);
            }
        }catch (Exception e){
            log.error("定时任务，超时15分钟订单关闭失败", e);
        }
    }
//    public void exec(){
//        try {
//            //TODO 待修改
//            if(null == payService){
//                log.info("定时任务，订单支付状态更新。应用未配置支付渠道，任务不执行。");
//                return;
//            }
//            List<String> orderIds = orderService.queryTimeoutCloseOrderList();
//            if(orderIds.isEmpty()){
//                log.info("定时任务，超时30分钟订单关闭，暂无超时未支付订单 orderIds is null");
//                return;
//            }
//            for(String orderId : orderIds){
//                boolean status = orderService.changeOrderClose(orderId);
//                //TODO 待修改
//                //微信关单；暂时不需要主动关闭
//                CloseOrderRequest request = new CloseOrderRequest();
//                request.setMchid(mchid);
//                request.setOutTradeNo(orderId);
//                payService.closeOrder(request);
//                log.info("定时任务，超时30分钟订单关闭 orderId: {} status：{}", orderId, status);
//            }
//        } catch (Exception e) {
//            log.error("定时任务，超时15分钟订单关闭失败", e);
//        }
//    }

}
